﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TARS.AdminCP
{
    public partial class Carrier : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Add data source if site if first load
            if (!Page.IsPostBack)
            {

                using (var db = new TARSDelivery_EprojectEntities())
                {
                    var getCarrierType = db.v_CarrierType.OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                    RadGrid1.DataSource = getCarrierType.ToList();
                    RadGrid1.DataBind();

                    //Add item for cboBranchesZone
                    RadComboBoxItem cboitem = new RadComboBoxItem();
                    cboitem.Text = "Select All";
                    cboitem.Value = "All";
                    cboBranchesZone.Items.Add(cboitem);

                    var getActiveBranchesZone = db.t_BrachesZone.Where(x => x.Active == true);
                    foreach (var get in getActiveBranchesZone)
                    {
                        cboitem = new RadComboBoxItem();
                        cboitem.Text = get.ZoneName;
                        cboitem.Value = get.ID.ToString();
                        cboBranchesZone.Items.Add(cboitem);
                    }
                }
            }
            else
            {
                //Add data source if site if post back
                using (var db = new TARSDelivery_EprojectEntities())
                {
                    var get = db.v_CarrierType.OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                    RadGrid1.DataSource = get.ToList();
                }
            }
        }

        protected void OnSelectedIndexChangedHandler(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //Get Branches Zone ID in combobox
            Session["BranchesZoneID"] = e.Value;
        }


        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {

            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                //Set hidden for ID & Created Time in Insert item and Update Item
                GridEditFormItem edit = (GridEditFormItem)e.Item;
                edit["NewID"].Visible = false;
                edit["CreatedTime"].Visible = false;


                //Set selected item in Update Item
                if (!(e.Item is IGridInsertItem))
                {

                    //Get control cboBranchesZone
                    GridEditableItem item = (GridEditableItem)e.Item;
                    RadComboBox combo = (RadComboBox)item.FindControl("cboBranchesZone");
                    string getzonename = ((v_CarrierType)e.Item.DataItem).ZoneName;


                    int AutoCount = 0;
                    int GetIndex = 0;
                    foreach (RadComboBoxItem getitem in combo.Items)
                    {
                        if (getitem.Text == getzonename)
                        {
                            GetIndex = AutoCount;
                        }
                        AutoCount++;
                    }
                    combo.SelectedIndex = GetIndex;
                    Session["BranchesZoneID"] = combo.SelectedItem.Value;
                }
                else
                {
                    //Set First Value of cboBranches for Session
                    GridEditableItem item = (GridEditableItem)e.Item;
                    RadComboBox combo = (RadComboBox)item.FindControl("cboBranchesZone");
                    Session["BranchesZoneID"] = combo.Items[0].Value;
                }
            }
        }

        protected void RadGrid1_UpdateCommand1(object sender, GridCommandEventArgs e)
        {
            //Update Item in database

            GridEditableItem item = e.Item as GridEditableItem;
            String CarrierTypeName = (item["CarrierTypeName"].Controls[0] as TextBox).Text; //Get Carrier Type Name in TextBox
            String CarrierTypeID = (item["NewID"].Controls[0] as TextBox).Text; //Get Carrier Type ID
            Boolean Active = (item["Active"].Controls[0] as CheckBox).Checked; //Get Active or Not
            Guid newguid = new Guid(CarrierTypeID);
            Guid newbrancheszoneid = new Guid(Session["BranchesZoneID"].ToString());

            //Update Command With Entity
            using (var db = new TARSDelivery_EprojectEntities())
            {
                t_CarrierType carrier = db.t_CarrierType.First(x => x.ID == newguid);
                carrier.CarrierTypeName = CarrierTypeName;
                carrier.BranchesZoneID = newbrancheszoneid;
                carrier.Active = Active;
                db.SaveChanges();

                var get = db.v_CarrierType.OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                RadGrid1.DataSource = get.ToList();
                RadGrid1.DataBind();
            }

            //Reset RadGrid
            RadGrid1.MasterTableView.ClearEditItems();
            RadGrid1.MasterTableView.ClearChildEditItems();
            RadGrid1.MasterTableView.ClearSelectedItems();
        }

        protected void RadGrid1_ItemCommand1(object sender, GridCommandEventArgs e)
        {
            //Open only Edit or Insert Item once
            if (e.CommandName == RadGrid.EditCommandName)
            {
                RadGrid1.MasterTableView.IsItemInserted = false;
            }

            //Set default values for Insert Item
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                RadGrid1.MasterTableView.ClearEditItems();
                Guid newguid = Guid.NewGuid();
                e.Canceled = true;
                Hashtable defaultvalues = new Hashtable();
                defaultvalues["NewID"] = newguid.ToString();
                defaultvalues["CreatedTime"] = DateTime.Now;
                defaultvalues["Active"] = true;
                e.Item.OwnerTableView.InsertItem(defaultvalues);
            }
        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {

            //Insert new Item

            GridEditableItem item = e.Item as GridEditableItem;
            String CarrierTypeName = (item["CarrierTypeName"].Controls[0] as TextBox).Text;
            String CarrierTypeID = (item["NewID"].Controls[0] as TextBox).Text;
            Boolean Active = (item["Active"].Controls[0] as CheckBox).Checked;
            Guid newguid = new Guid(CarrierTypeID);
            Guid newbrancheszoneid = new Guid(Session["BranchesZoneID"].ToString());
            using (var db = new TARSDelivery_EprojectEntities())
            {
                t_CarrierType carrier = new t_CarrierType();
                carrier.ID = newguid;
                carrier.CarrierTypeName = CarrierTypeName;
                carrier.BranchesZoneID = newbrancheszoneid;
                carrier.Active = Active;
                carrier.CreatedTime = DateTime.Now;
                db.t_CarrierType.Add(carrier);
                db.SaveChanges();

                //Reset RadGrid
                RadGrid1.MasterTableView.ClearEditItems();
                RadGrid1.MasterTableView.ClearChildEditItems();
                RadGrid1.MasterTableView.ClearSelectedItems();

                var get = db.v_CarrierType.OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                RadGrid1.DataSource = get.ToList();
            }

        }

        protected void cboBranchesZone_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //Filter RadGrid with combobox Branches Zone

            using (var db = new TARSDelivery_EprojectEntities())
            {
                //If selected item is not "Select All"
                if (e.Value != "All")
                {
                    Guid getBranchesID = new Guid(e.Value);
                    var getCarrierType = db.v_CarrierType.Where(x => x.ZoneName == e.Text.Trim());
                    RadGrid1.DataSource = getCarrierType.ToList();
                    RadGrid1.Rebind();
                }
                else
                {
                    var get = db.v_CarrierType.OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                    RadGrid1.DataSource = get.ToList();
                    RadGrid1.Rebind();
                }
            }
        }

        protected void RadButton1_Click(object sender, EventArgs e)
        {

        }
    }
}